/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.swing;

import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;

/**
 * Simple Icon implementation of using two existing icons as one side by side.
 * <p>
 * 
 * @author Mark A. Kobold
 * @version 1.0
 */
public class CompoundIcon implements Icon {

    private static int lastX = 0;
    private static int lastY = 0;

    protected Icon right;
    protected int iconGap = 2;
    protected Icon left;

    /**
     * Default constructor for this object.
     * <p>
     * Either parameter for this method can be null if need be however if both icons are null this won't show much ?
     * 
     * @param leftIcon icon to be shown on the left side.
     * @param rightIcon icon to be shown on the right side.
     */
    public CompoundIcon(Icon leftIcon, Icon rightIcon) {

        left = leftIcon;
        right = rightIcon;
    }

    /**
     * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
     */
    public void paintIcon(Component c, Graphics g, int x, int y) {

        lastX = x;
        lastY = y;
        if (left != null)
            left.paintIcon(c, g, x, y);

        if (right != null) {
            int l = (left == null ? 0 : (left.getIconWidth() + iconGap));
            right.paintIcon(c, g, x + l, y);
        }
    }

    /**
     * @see javax.swing.Icon#getIconWidth()
     */
    public int getIconWidth() {

        int l = (left == null ? 0 : left.getIconWidth());
        int r = (right == null ? 0 : right.getIconWidth());
        return l + r + iconGap;
    }

    /**
     * @see javax.swing.Icon#getIconHeight()
     */
    public int getIconHeight() {

        int l = (left == null ? 0 : left.getIconHeight());
        int r = (right == null ? 0 : right.getIconHeight());

        return Math.max(r, l);
    }

    /**
     * Returns the instance of the icon drawn on thr right side.
     * <p>
     * 
     * @return Icon shown on the right.
     */
    public Icon getRightIcon() {

        return right;
    }

    /**
     * Returns the instance of the icon drawn on thr left side.
     * <p>
     * 
     * @return Icon shown on the left.
     */
    public Icon getLeftIcon() {

        return left;
    }

    /**
     * Replaces the icon on the right side.
     * <p>
     * 
     * @param Icon to be shown on the right.
     */
    public void setRightIcon(Icon icon) {

        right = icon;
    }

    /**
     * Replaces the icon on the left side.
     * <p>
     * 
     * @param Icon to be shown on the left.
     */
    public void setLeftIcon(Icon icon) {

        left = icon;
    }

    /**
     * This method is used to see if the relative coordinates are contained in the left icon.
     * <p>
     * If the left icon is null this method will always return false.
     * 
     * @param x relative x coordinate of this compond icon.
     * @param y relative y coordinate of this compond icon.
     * @return boolean if x and y are within the bounds of the left icon.
     */
    public boolean isOverIcon(int x, int y) {

        int w = (left == null ? 0 : left.getIconWidth());
        int h = (left == null ? 0 : left.getIconHeight());

        if (!((x >= lastX) && (x <= (lastX + w))))
            return false;
        if (!((y >= lastY) && (y <= (lastX + h))))
            return false;

        return true;
    }
}